<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="100px"
    >
      <el-form-item label="订单号" prop="orderNumber">
        <el-input
          v-model="queryParams.orderNumber"
          placeholder="请输入订单号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="订单类型" prop="dictOrderType">
        <el-select
          v-model="queryParams.dictOrderType"
          placeholder="请选择下料工种"
          clearable
          class="!w-220px"
        >
          <el-option
            v-for="(dict, index) in getIntDictOptions('market_order_type')"
            :key="index"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="客户代码" prop="customerCode">
        <el-input
          v-model="queryParams.customerCode"
          placeholder="请输入客户代码"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="客户名称" prop="customerName">
        <el-input
          v-model="queryParams.customerName"
          placeholder="请输入客户名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="产品编号" prop="productNumber">
        <el-input
          v-model="queryParams.productNumber"
          placeholder="请输入产品编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="产品名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入产品名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="产品规格" prop="productStandard">
        <el-input
          v-model="queryParams.productStandard"
          placeholder="请输入产品规格"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="部件编码" prop="partNumber">
        <el-input
          v-model="queryParams.partNumber"
          placeholder="请输入部件编码"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="部件名称" prop="partName">
        <el-input
          v-model="queryParams.partName"
          placeholder="请输入部件名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="发货单号" prop="trackingNumber">
        <el-input
          v-model="queryParams.trackingNumber"
          placeholder="请输入发货单号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="部件规格" prop="partSpecification">
        <el-input
          v-model="queryParams.partSpecification"
          placeholder="请输入部件规格"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      <!-- <el-form-item label="入库类型" prop="dictIncomingType">
        <el-select
          v-model="queryParams.dictIncomingType"
          placeholder="请选择入库类型"
          clearable
          class="!w-220px"
        >
          <el-option
            v-for="(dict, index) in getIntDictOptions('incoming_type')"
            :key="index"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item> -->

      <el-form-item>
        <el-button type="primary" @click="handleQuery">搜索</el-button>
        <el-button @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <div class="operateClick">
      <div>
        <el-button
          v-hasPermi="['produce:productOutbound:export']"
          type="primary"
          :loading="exportLoading"
          @click="handleExport"
        >
          数据导出
        </el-button>
      </div>
      <div class="titleSet">
        <el-button type="primary" @click="openTableTitleSet"> 表头设置 </el-button>
      </div>
    </div>
    <Table
      :columns="tableTitleList"
      :selection="tableObject.tableList.length ? true : false"
      @register="tableoperation"
      :data="tableObject.tableList"
      :loading="tableObject.loading"
      :pagination="{
        total: tableObject.total
      }"
      v-model:pageSize="tableObject.pageSize"
      v-model:currentPage="tableObject.currentPage"
      :tableHeight="tableHeight"
    >
      <template #action="{ row }">
        <el-button
          link
          type="primary"
          @click="openForm('view', row.id, row.trackingNumber)"
          v-hasPermi="['produce:production-incoming:view']"
        >
          查看
        </el-button>
      </template>
    </Table>
  </ContentWrap>

  <!-- 导出 -->
  <ExportFile ref="exportRef" @download="downloadFun" />
  <!-- 表头设置 -->
  <table-width-set ref="tableTitleSet" @refresh="getLabelAll" />
</template>

<script setup lang="ts" name="ProductOutbound">
  import { getIntDictOptions } from '@/utils/dict'
  // import { dateFormatter } from '@/utils/formatTime'
  import download from '@/utils/download'
  import * as ProductOutboundApi from '@/api/produce/productManagement/productOutbound'
  // const message = useMessage() // 消息弹窗
  // const { t } = useI18n() // 国际化
  const tableTitleList = ref([]) // 表头的数据

  const queryParams = reactive({
    pageNo: 1,
    pageSize: 10,
    orderNumber: null,
    dictOrderType: undefined,
    customerCode: null,
    customerName: null,
    productNumber: null,
    productName: null,
    productStandard: null,
    partNumber: null,
    partName: null,
    partSpecification: null,
    orderQuantity: null,
    incomingQuantity: null,
    dictUnit: null,
    dictIncomingType: undefined,
    warehouseCode: null,
    warehouseName: null,
    creatorName: null,
    createTime: [] as any,
    updaterName: null,
    trackingNumber: null
  })
  const queryFormRef = ref() // 搜索的表单
  const exportLoading = ref(false) // 导出的加载中

  /** 查询列表 */
  const { tableObject, tableMethods } = useTable({
    getListApi: ProductOutboundApi.getProductionOutingPage, // 分页接口
    defaultParams: queryParams
  })

  /** 查询列表 */
  const getLabelAll = async () => {
    try {
      const data = await ProductOutboundApi.getProductionOutingPage(queryParams)
      let json = {
        label: '操作',
        field: 'action',
        isDetail: false,
        width: 80
      }
      data.labels.push(json)
      const { allSchemas } = useCrudSchemas(data.labels)
      tableTitleList.value = allSchemas.tableColumns as any
    } finally {
    }
  }

  // 获得表格的各种操作
  const { getList } = tableMethods

  /** 搜索按钮操作 */
  const handleQuery = () => {
    queryParams.pageNo = 1
    getList()
  }

  /** 重置按钮操作 */
  const resetQuery = () => {
    queryFormRef.value.resetFields()
    handleQuery()
  }

  /**表头内部方法 */
  const usTableoperation = ref()
  const tableoperation = (parent: any) => {
    usTableoperation.value = parent
  }

  /**打开表格表头设置**/
  const tableTitleSet = ref()
  const openTableTitleSet = () => {
    let code = 'ProductOutbound'
    tableTitleSet.value.open(tableTitleList.value, code)
  }

  /** 添加/修改操作 */
  const { push } = useRouter() // 路由
  const openForm = (type: string, id?: number, trackingNumber) => {
    push({
      path: '/produce/produce/productManagement/productOutboundDetail',
      query: {
        trackingNumber,
        id,
        type
      }
    })
  }

  /** 导出按钮操作 */
  const exportRef = ref()
  const handleExport = async () => {
    exportRef.value.open()
  }

  // 下载
  const downloadFun = async () => {
    const val = exportRef.value.params
    let params = {
      fileName: val.name,
      ids: ''
    }
    if (val.range === 'all') {
      params.ids = ''
    } else {
      usTableoperation.value.selections.map((item) => {
        params.ids = (item as any).id + ',' + params.ids
      })
    }
    const data = await ProductOutboundApi.exportProductionOuting(params)
    download.excel(data, val.name)
    clearSelectData()
  }

  //清空多选
  const clearSelectData = () => {
    usTableoperation.value.selections = []
    usTableoperation.value.clearTableSelect()
  }

  /** 初始化 **/
  const tableHeight = ref(0)
  onMounted(async () => {
    const queryParamsHeight = queryFormRef.value.$el.clientHeight + 2
    tableHeight.value = document.body.clientHeight - 132 - queryParamsHeight - 127
    await getLabelAll()
    getList()
  })
</script>
<style lang="scss" scoped>
  .operateClick {
    height: 50px;
    display: flex;
    justify-content: space-between;
    .titleSet {
      display: flex;
      justify-content: flex-end;
      // text-align: right;
    }
  }

  :deep(.el-select .el-input) {
    width: 100%;
  }
</style>
